<?php
/**
 * polwebgis
 * 
 * This file is part of polwebgis.
 *
 * @copyright   Copyright (c) 2012 Dominik Janisch (http://www.joanneum.at)
 * @lastmodified    $LastChangedDate: 2012-03-07 09:15:12 +0000 (Wed, 07 Mar 2012) $
 * @revision    $Revision: 160 $ 
 */

require_once(PATH_TO_APP . "/components/gismap/InputMapper.php");
require_once(PATH_TO_APP . "/components/gismap/RegionMap.php");
require_once(PATH_TO_APP . "/components/search/Search.php");
require_once(PATH_TO_APP . "/components/basetablehandler/Layer.php");
require_once(PATH_TO_APP . "/components/basetablehandler/tabledatagateway/LayerGateway.php");
require_once(PATH_TO_APP . "/components/basetablehandler/Region.php");
require_once(PATH_TO_APP . "/components/basetablehandler/tabledatagateway/RegionGateway.php");
require_once(PATH_TO_APP . "/components/basetablehandler/Topic.php");
require_once(PATH_TO_APP . "/components/basetablehandler/tabledatagateway/TopicGateway.php");
require_once(PATH_TO_APP . "/components/tabledatagateway/TableDataGateway.php");
require_once(PATH_TO_APP . "/components/gismap/tabledatagateway/InputMapperGateway.php");
require_once(PATH_TO_APP . "/components/tabledatagateway/InputHandlerGateway.php");
require_once(PATH_TO_APP . "/components/tabledatagateway/RegionMapGateway.php");
require_once(PATH_TO_APP . "/components/search/tabledatagateway/SearchGateway.php");

class InputHandler
{
    private $table_gateway_;
    private $database_connection_;
    private $output_handler_;
    private $request_;
    private $singleton_registry_;
    private $language_handler_;

    public function __construct($singleton_registry)
    {
        $this->singleton_registry_ = $singleton_registry;
        $this->database_connection_ = $singleton_registry->getSingleton('DatabaseConnection');
        $this->output_handler_ = $singleton_registry->getSingleton('OutputHandler');
        $this->request_ = $singleton_registry->getSingleton('Request');
        $this->language_handler_ = $singleton_registry->getSingleton('LanguageHandler');
    }
	
    public function __destruct(){}
    
    public function run()
    {
        $this->database_connection_->newConnection(PG_CONNECTION_STRING);
        $this->table_gateway_ = new InputHandlerGateway();
        $this->language_handler_->assignLanguageFile('DE', $this->output_handler_);
        $this->output_handler_->assignValuesToTemplate(array("base_uri" => BASE_URI));
        $input = $this->request_->getParameter('get', 'input');
        try{
            switch($input)
            {
                case 'wms':
                    $im = new InputMapper(new InputMapperGateway(), $this->singleton_registry_);
                    $map_errorless = $im->checkInput(true);
                    break;
                case 'map':
                    $im = new InputMapper(new InputMapperGateway(), $this->singleton_registry_);
                    $map_errorless = $im->checkInput(false);
                    break;
                case 'regionwms':
                    $rm = new RegionMap(true, new RegionMapGateway(), $this->singleton_registry_);
                    $map_errorless = $rm->init();
                    break;
                case 'region':
                    $rm = new RegionMap(false, new RegionMapGateway(), $this->singleton_registry_);
                    $map_errorless = $rm->init();
                    break;
                case 'imprint':
                    $this->output_handler_->showTemplate("imprint");
                    break;
                case 'feed':
                    $this->showFeed();
                    break;
                case 'search':
                    $search = new Search(new SearchGateway(), $this->singleton_registry_);
                    $search->search();
                    break;
                case 'project':
                    $this->showMainpage(false);
                    break;
                case 'languagestring':
                    $this->getLanguageString();
                    break;
                default:
                    if($this->request_->issetParameter('get', 'REQUEST'))
                    {
                        $im = new InputMapper(new InputMapperGateway(), $this->singleton_registry_);
                        $map_errorless = $im->checkInput(true);
                    }
                    else
                        $this->showMainpage(true);
            }
        }
        catch(Exception $e)
        {
            $this->output_handler_->assignValuesToTemplate(array("error_message" => $e->getMessage()));
            $this->output_handler_->showTemplate("main");
        }
    }
    
    private function showMainPage($is_global=true)
    {
        $is_global = (boolean)$is_global;
        $project_title = '';
        $page_title = $is_global ? "Globalthemen" : "Projekte";
        $t_id = $this->request_->issetParameter('get', 't_id') ? $this->request_->getParameter('get', 't_id') : 0;
        $t_id = $this->request_->issetParameter('post', 't_id') ? $this->request_->getParameter('post', 't_id') : $t_id;
        $ajax = $this->request_->issetParameter('get', 'ajax') ? true : false;
        $topic = new Topic(new TopicGateway());
        if($is_global)
        {
            $arr_datatables = $this->table_gateway_->getGlobalDataTables();
            $t_id = $topic->getFirstTopic();
        }
        else
        {
            if($t_id == 0)
                $t_id = $topic->getFirstTopic();
            $project_title = $topic->getTopicName($t_id);
            $arr_datatables = $this->table_gateway_->getDataTables($t_id);
        }
        $layer = new Layer(new LayerGateway());
        $region = new Region(new RegionGateway());
        $tags = new Tags(new TagsGateway());
        $this->output_handler_->assignValuesToTemplate(array("arr_datatables" => $arr_datatables, "t_id" => $t_id, "arr_layers" => $layer->getLayers(), "arr_regions" => $region->getRegions(), "arr_verified_topics" =>  $tags->getTagCategories(), "arr_topics" => $topic->getTopics(), "is_global" => $is_global, "page_title" => $page_title, "ajax" => $ajax, "project_title" => $project_title));
        $this->output_handler_->showTemplate("main");
    }
    
    private function showFeed()
    {
        $this->output_handler_->assignValuesToTemplate(array("arr_data" => $this->table_gateway_->getFeedData()));
        $this->output_handler_->showTemplate("rss");
    }
    
    private function getLanguageString()
    {
        $arr_strings = explode(',', $this->request_->getParameter('get', 'language_string'));
        $arr_return = array();
        foreach($arr_strings as $string)
        {
            $arr_return[$string] = $this->language_handler_->getLanguageString($string);
        }
        $this->output_handler_->showJSON($arr_return);
    }
}
?>
